// pages/login/index.js
const cloudHelper = require('../../helper/cloud_helper.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        ok: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    // 勾选用户协议
    onCheckboxChange(e) {
        let value = e.detail.value[0];
        this.setData({
            ok: value ? true : false
        });
    },

    // 跳转用户协议
    goAgreement() {
        wx.navigateTo({
            url: '/pages/other/agreement/index'
        })
    },

    // 检查是否勾选用户协议
    checkAgree() {
        if (!this.data.ok) {
            wx.showToast({
                title: '请勾选我同意服务协议'
            })
            return false;
        } else {
            return true;
        }
    },

    // 手机授权登陆
    async phoneLogin(e) {
        if (!this.checkAgree()) {
            return false;
        }
        if (e.detail.errMsg != "getPhoneNumber:ok") {
            wx.showToast({
                title: '授权失败',
                icon: 'error'
            });
            return;
        }
        const spread_openid = wx.getStorageSync("spread_openid");
        let params = {
            cloudID: e.detail.cloudID,
            spread_openid: spread_openid
        };
        let res = await cloudHelper.callCloudData('api/user/phone_register', params);
        if (res.open_id) {
            wx.navigateBack();
        } else {
            wx.showToast({
                title: '登陆异常',
                icon: 'error'
            });
            return;
        }
    },

})